package com.wyx.mhl.view;

import com.wyx.mhl.domain.*;
import com.wyx.mhl.service.BillService;
import com.wyx.mhl.service.DiningTableService;
import com.wyx.mhl.service.EmployeeService;
import com.wyx.mhl.service.MenuService;
import com.wyx.mhl.utils.Utility;

import java.util.List;

/**
 * @author 王艺锡
 * @version 1.0
 * 主界面
 */
public class MHLView {
    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //控制主菜单
    private boolean flag = true;
    private String key = "";

    //定义 EmployeeService 属性
    private EmployeeService employeeService = new EmployeeService();
    //定义 DiningTableService 属性
    private DiningTableService diningTableService = new DiningTableService();
    //定义 MenuService 属性
    private MenuService menuService = new MenuService();
    //定义 BillService 属性
    private BillService billService = new BillService();


    //显示所有餐桌信息
    public void tableInfo() {
        List<diningTable> diningTables = diningTableService.tableInfo();
        System.out.println("=========当前餐桌信息=========");
        System.out.println("   餐桌编号\t\t  餐桌状态");
        for (diningTable diningTable : diningTables) {
            System.out.println(diningTable);
        }
        System.out.println("============================");
    }


    //预定餐桌
    public void bookDiningTable() {
        System.out.println("=========预定餐桌=========");
        System.out.print("请选择要预定的餐桌编号(-1退出):");
        int id = Utility.readInt();
        if (id == -1) {
            System.out.println("取消预订...");
            return;
        }

        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            diningTable diningTable = diningTableService.getDiningTable(id);
            if (diningTable == null) {
                System.out.println("餐桌编号输入错误!");
                return;
            }
            if (!(diningTable.getState().equals("空"))) {
                System.out.println("预定失败，当前餐桌已被预定!");
                return;
            }

            System.out.print("请输入预订人名称:");
            String orderName = Utility.readString(50);
            System.out.print("请输入预订人电话:");
            String orderTel = Utility.readString(20);


            if (diningTableService.updateDiningTable(id, orderName, orderTel)) {
                System.out.println("预定成功！");
            } else {
                System.out.println("预定失败!");
            }

        } else {
            System.out.println("取消预订...");
        }
    }


    //显示所有菜品
    public void getAllMenu() {
        System.out.println("菜品编号\t\t菜品名\t\t类别\t\t价格");
        List<menu> allMenu = menuService.getAllMenu();
        for (menu menu : allMenu) {
            System.out.println(menu);
        }
        System.out.println("=====================================");
    }


    //点餐
    public void order() {
        System.out.print("请输入已预订的桌位号(-1退出):");
        int orderTableID = Utility.readInt();
        if (orderTableID == -1) {
            System.out.println("退出点餐...");
            return;
        }
        System.out.print("请输入预订人姓名:");
        String orderName = Utility.readString(50);
        //判断餐桌是否为空，餐桌状态是否为已预订或就餐中，预定人姓名是否正确
        diningTable diningTable = diningTableService.getDiningTable(orderTableID);
        if (!(diningTable != null && (diningTable.getState().equals("已预订") || diningTable.getState().equals("就餐中")) && orderName.equals(diningTable.getOrderName()))) {
            System.out.println("您的预定信息有误...");
            return;
        }

        System.out.print("请输入菜品号(-1退出):");
        int orderMenuID = Utility.readInt();
        if (orderMenuID == -1) {
            System.out.println("退出点餐...");
            return;
        }
        //判断菜品号是否正确
        menu singleMenu = menuService.getSingleMenu(orderMenuID);
        if (singleMenu == null) {
            System.out.println("您输入的菜品号有误...");
            return;
        }
        System.out.print("请输入菜品数量(-1退出):");
        int orderNums = Utility.readInt();
        if (orderNums == -1) {
            System.out.println("退出点餐...");
            return;
        }

        //点餐
        if (billService.order(orderMenuID, orderNums, orderTableID)) {
            System.out.println("点餐成功!");
        } else {
            System.out.println("点餐失败!");
        }

    }


    //查看账单
    public void getAllBill(){
        System.out.println("编号\t\t菜品号\t菜品量\t金额\t\t\t桌号\t\t日期\t\t\t\t状态");
        List<bill> allBill = billService.getAllBill();
        for (bill bill :allBill) {
            System.out.println(bill);
        }

    }


    //查看账单2
    public void getAllBill2(){
        System.out.println("编号\t\t菜品号\t菜品量\t金额\t\t\t桌号\t\t日期\t\t\t\t状态\t\t\t菜品名");
        List<multiTableBean> allBill = billService.getAllBill2();
        for (multiTableBean bill :allBill) {
            System.out.println(bill);
        }

    }




    //结账
    public void checkout(){
        System.out.print("请输入要结账的餐桌号(-1退出):");
        int tableId = Utility.readInt();
        if(tableId == -1){
            System.out.println("退出结账...");
            return;
        }

        //判断
        diningTable diningTable = diningTableService.getDiningTable(tableId);
        if(!(diningTable != null && diningTable.getState().equals("就餐中"))){
            System.out.println("结账失败,餐桌未处于就餐中...");
            return;
        }

        //合计消费
        double total = billService.total(tableId);
        System.out.println("该桌总共消费:" + total + "元");
        System.out.print("请选择结账方式(现金/支付宝/微信):");
        String payMode = Utility.readString(20);

        //修改表中数据
        if(billService.payBill(tableId,payMode)){
            System.out.println("结账成功,欢迎下次再来!");
        }else {
            System.out.println("结账失败!");
        }
    }

    //主菜单
    public void mainMenu() {
        while (flag) {
            System.out.println("==========满汉楼==========");
            System.out.println("\t\t1.登录满汉楼");
            System.out.println("\t\t0.退出满汉楼");
            System.out.print("请输入:");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("请输入员工号:");
                    String empId = Utility.readString(50);
                    System.out.print("请输入密  码:");
                    String pwd = Utility.readString(50);
                    employee emp = employeeService.enroll(empId, pwd);
                    if (emp != null) {
                        System.out.println("登录成功...");
                        while (flag) {
                            System.out.println("\n================满汉楼二级菜单[" + emp.getName() + "]================");
                            System.out.println("\t\t1.显示餐桌状态");
                            System.out.println("\t\t2.预定餐桌");
                            System.out.println("\t\t3.显示所有菜品");
                            System.out.println("\t\t4.点餐");
                            System.out.println("\t\t5.查看账单");
                            System.out.println("\t\t6.结账");
                            System.out.println("\t\t0.退出满汉楼");
                            System.out.print("请输入你的选择:");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    tableInfo();
                                    break;
                                case "2":
                                    bookDiningTable();
                                    break;
                                case "3":
                                    getAllMenu();
                                    break;
                                case "4":
                                    order();
                                    break;
                                case "5":
                                    getAllBill2();
                                    break;
                                case "6":
                                    checkout();
                                    break;
                                case "0":
                                    System.out.println("退出满汉楼");
                                    flag = false;
                                    break;
                                default:
                                    System.out.println("输入错误，请重新输入!");
                                    break;
                            }


                        }
                    } else {
                        System.out.println("登录失败...");
                    }
                    break;
                case "0":
                    System.out.println("退出满汉楼...");
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，请重新输入!");
                    break;
            }

        }

    }

}
